/**
 * fulihui.com Inc.
 * Copyright (c) 2015-2016 All Rights Reserved.
 */
package com.fulihui.integral.core.dal.repository.impl;

import com.fulihui.integral.core.biz.conv.ISUserMonthStatConvert;
import com.fulihui.integral.core.dal.dataobj.ISUserMonthStat;
import com.fulihui.integral.core.dal.dataobj.ISUserMonthStatExample;
import com.fulihui.integral.core.dal.mapper.ISUserMonthStatMapper;
import com.fulihui.integral.core.dal.mapper.ext.ISUserMonthStatExtMapper;
import com.fulihui.integral.core.dal.repository.ISUserMonthStatRepository;
import com.fulihui.integral.core.dto.ISUserMonthStatDTO;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.inject.Inject;
import java.util.List;

/**
 * @author zhangyingjie
 * @version $Id: v 0.1 2017年04月14日 17:23 zhangyingjie Exp $
 */
@Repository
public class ISUserMonthStatRepositoryImpl implements ISUserMonthStatRepository {
    @Autowired
    private ISUserMonthStatMapper iSUserMonthStatMapper;
    @Autowired
    private ISUserMonthStatExtMapper iSUserMonthStatExtMapper;

    @Override
    public boolean insert(ISUserMonthStatDTO insert) {
        int insert1 = iSUserMonthStatMapper.insert(ISUserMonthStatConvert.convertDTO2DO(insert));
        if (insert1 != 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean update(ISUserMonthStatDTO update) {
        int update1 = iSUserMonthStatMapper.updateByPrimaryKey(ISUserMonthStatConvert.convertDTO2DO(update));
        if (update1 != 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean insertOrUpdate(ISUserMonthStatDTO dto) {
        if (dto == null) {
            return false;
        }
        if (dto.getId() != null && dto.getId() != 0) {
            return update(dto);
        } else {
            return insert(dto);
        }
    }

    @Override
    public ISUserMonthStatDTO selectByStatTimeAndUserId(String statTime, String userId) {
        ISUserMonthStatExample example = new ISUserMonthStatExample();
        ISUserMonthStatExample.Criteria criteria = example.createCriteria();
        criteria.andStatTimeEqualTo(statTime);
        criteria.andUserIdEqualTo(userId);
        List<ISUserMonthStat> isUserMonthStats = iSUserMonthStatMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(isUserMonthStats)) {
            return ISUserMonthStatConvert.convertDO2DTO(isUserMonthStats.get(0));
        }
        return null;
    }
}
